<?php

namespace app\admin\model;

use think\Model;


class Banner extends Model
{

    

    

    // 表名
    protected $table = 'banner';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'file_type_text',
        'jump_type_text',
        'jump_url_type_text',
        'start_time_text',
        'end_time_text',
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getFileTypeList()
    {
        return ['image' => __('file_type image'), 'video' => __('file_type video')];
    }


    public function getJumpTypeList()
    {
        return ['mini' => __('Jump_type mini')];
        return ['mini' => __('Jump_type mini'), 'pc' => __('Jump_type pc')];
    }

    public function getJumpUrlTypeList()
    {
        return ['without' => __('Jump_url_type without'), 'inside' => __('Jump_url_type inside')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getFileTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['file_type']) ? $data['file_type'] : '');
        $list = $this->getFileTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getJumpTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['jump_type']) ? $data['jump_type'] : '');
        $list = $this->getJumpTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getJumpUrlTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['jump_url_type']) ? $data['jump_url_type'] : '');
        $list = $this->getJumpUrlTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
